package org.example.traffic.organizationadmin;

import org.example.traffic.organization.Organization;
import org.example.traffic.organizationadmin.OrganizationAdmin;
import org.example.traffic.organizationadmin.OrganizationAdminService;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;
/**
 * @author: wyk
 */
@RestController
@RequestMapping("/organization-admin")
public class OrganizationAdminController {
    private final OrganizationAdminService organizationAdminService;

    public OrganizationAdminController(OrganizationAdminService organizationAdminService) {
        this.organizationAdminService = organizationAdminService;
    }

    // 注册接口，接收前端传来的组织管理员信息并保存到数据库
    @PostMapping("/register")
    public ResponseEntity<OrganizationAdmin> register(@RequestBody OrganizationAdmin organizationAdmin) {
        OrganizationAdmin registeredAdmin = organizationAdminService.register(organizationAdmin);
        return new ResponseEntity<>(registeredAdmin, HttpStatus.CREATED);
    }

    // 登录接口，接收用户名和密码，验证并返回相应结果
    @PostMapping("/login")
    public ResponseEntity<String> login(@RequestBody OrganizationAdmin organizationAdmin) {
        Optional<OrganizationAdmin> loggedInAdmin = organizationAdminService.login(organizationAdmin.getUsername(), organizationAdmin.getPassword());
        if (loggedInAdmin.isPresent()) {
            return ResponseEntity.ok().body("Login successful");
        }
        return new ResponseEntity<>("Login failed", HttpStatus.UNAUTHORIZED);
    }

    // 新增组织管理员接口
    @PostMapping
    public ResponseEntity<OrganizationAdmin> create(@RequestBody OrganizationAdmin organizationAdmin) {
        OrganizationAdmin savedAdmin = organizationAdminService.save(organizationAdmin);
        return new ResponseEntity<>(savedAdmin, HttpStatus.CREATED);
    }

    // 根据ID获取组织管理员接口
    @GetMapping("/{id}")
    public ResponseEntity<Optional<OrganizationAdmin>> getById(@PathVariable Integer id) {
        Optional<OrganizationAdmin> organizationAdmin = organizationAdminService.findById(id);
        if (organizationAdmin.isPresent()) {
            return new ResponseEntity<>(organizationAdmin, HttpStatus.OK);
        }
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }

    // 分页查询视频
    @GetMapping
    public ResponseEntity<Page<OrganizationAdmin>> getOrganizations(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "5") int size) {
        Page<OrganizationAdmin> organizations = organizationAdminService.getAdmins(page-1, size);
        return ResponseEntity.ok(organizations);
    }

    // 更新组织管理员接口
    @PutMapping("/{id}")
    public ResponseEntity<OrganizationAdmin> update(@PathVariable Integer id, @RequestBody OrganizationAdmin organizationAdmin) {
        organizationAdmin.setId(id);
        OrganizationAdmin updatedAdmin = organizationAdminService.update(organizationAdmin);
        return new ResponseEntity<>(updatedAdmin, HttpStatus.OK);
    }

    // 删除组织管理员接口
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteById(@PathVariable Integer id) {
        organizationAdminService.deleteById(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
    // 根据用户名模糊查询
    @GetMapping("/search")
    public ResponseEntity<Page<OrganizationAdmin>> findByUsernameContaining(
            @RequestParam String username,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "5") int size) {
        Page<OrganizationAdmin> admins = organizationAdminService.findByUsernameContaining(username, page-1, size);
        return ResponseEntity.ok(admins);
    }
    // 异常处理方法，处理服务层抛出的用户名已存在的异常(在新增、注册、更新组织管理员时都不能使用已存在的账号)
    @ExceptionHandler(IllegalArgumentException.class)
    public ResponseEntity<String> handleIllegalArgumentException(IllegalArgumentException e) {
        return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
    }

}
